//
//  SRPacket.h
//  sr01sdkProject
//
//  Created by  on 2022/5/7.
//  打包命令

#import <Foundation/Foundation.h>
#import "CABLeService.h"
#import "CADeviceInfo.h"
#import "CAEcgData.h"
#import "CAEcgAlgorithmResults.h"
#import "CAnAlgSwimSumData.h"
#import "CAnAlgSwimLapData.h"
#import "CAnAlgExerVitalSignsData.h"

NS_ASSUME_NONNULL_BEGIN

typedef NS_ENUM(UInt8, PACK_CMD) {
    PACK_CMD_SET_SPORT_MODE = 0x01,         //运动模式设置 废弃
    PACK_CMD_SET_HR_TEMP_MEASURE = 0x02,    // 体温 心率设置
    PACK_CMD_SET_OXYGEN_MEASURE = 0x03,     //血氧测量设置
    PACK_CMD_SYNC_TIME = 0x04,              // 时间同步
    PACK_CMD_GET_OXYGEN_HRV = 0x05,         // 获取心率血氧
    PACK_CMD_GET_STEPS = 0x06,              //获取计步
    PACK_CMD_GET_TEMPERATURE = 0x07,        //获取体温
    PACK_CMD_SHUT_DOWN = 0X08,              // 关机
    PACK_CMD_REBOOT = 0X09,                 // 重启
  
    PACK_CMD_FACTORY_RESET = 0x0a,          // 恢复出厂设置
    
    PACK_CMD_HIS_COUNT = 0x0c,              // 历史数据个数
    PACK_CMD_HIS_DATA = 0x0d,               // 历史数据
    PACK_CMD_CLEAR_HIS_DATA = 0x0e,         // 清空历史
    PACK_CMD_DEVICE_INFO = 0x0f,            // 设备信息
    PACK_CMD_SN = 0x10,                     // sn
    PACK_CMD_BATTERY = 0X11,                // 电池电量
    PACK_CMD_SOS_SETTING = 0X13,            // sos setting
    PACK_CMD_BIND = 0X15,                   // 绑定解绑设备
    PACK_CMD_OEM_SWITCH = 0x19,             // oem认证的开关
    PACK_CMD_START_MANUFACT_CER = 0x1B,     // 开始厂家验证
    PACK_CMD_MANUFACT_SEND_R2 = 0x1C,       // 向设备发送解密原文
    PACK_CMD_MODIFY_BRCAST_NAME = 0x1D,     // 修改广播名称
    PACK_CMD_SPORT_MODE = 0x20,             // 非newAlgo 运动模式
    PACK_CMD_FUNCTIN_SWITCH = 0x31,         // 设备功能开关
    PACK_CMD_FUNCTIN_SET_HR_MEASURE_DURATION = 0x3C, // 设置测量时长
    PACK_CMD_FUNCTIN_GET_DEVICEINFO_5 = 0x3d, // 获取设备信息5 心率测量时长 回包0x8f
    PACK_CMD_FUNCTIN_SET_STEP_THRESHOLD = 0x3F, // 设置计步阈值
    PACK_CMD_FUNCTIN_ECG = 0x52,             // 仅sr23可用 回包 0x94
    PACK_CMD_FUNCTIN_ECG_PPG = 0x53,         // 仅sr23可用  回包 0x85
    /* newalgo */
    PACK_CMD_FUNCTIN_SET_USERINFO = 0x58, //设置用户信息到设备,用于newalgo算法
    PACK_CMD_FUNCTIN_START_CM_EXER = 0x59, //开启/关闭 newalgo锻炼
    
    PACK_CMD_FUNCTIN_GET_HISCNT_ACT_SWIM_SLEEP  = 0x5A, // 获取活动检测/游泳/睡眠历史数据数量
    PACK_CMD_FUNCTIN_GET_HIS_DATA_ACT_SWIM_SLEEP  = 0x5B, // 获取活动检测/游泳/睡眠历史数据
    PACK_CMD_FUNCTIN_GET_LAST_ACTIVITY = 0x5C, //获取最后的活动 应答0xC8

    PACK_CMD_FUNCTIN_CLEAN_ACTIVITY_DATA = 0x5E, // 清空活动历史数据
    PACK_CMD_FUNCTIN_EXER_REALTM_REPORT = 0x5F, // 锻炼数据实时上报
    PACK_CMD_FUNCTIN_CM_MEA_DURA_AND_TIME = 0x60, // 设置/查询 newalgo设备 测量时间和测量间隔时间

    PACK_CMD_FUNCTIN_CM_DEVICE_ALGORITHM = 0x61, // 设置/查询 设备算法类型
    PACK_CMD_FUNCTIN_LED_MEA = 0x63,  // 单色led测量 ppg测量, 用于血糖血压
    
    PACK_CMD_FUNCTIN_GET_LAST_ACTIVITY_2 = 0x68, //获取最后的活动 应答0xD3, 0xD4

};// 下发命令字

typedef NS_ENUM(UInt8, DEVICE_RESPONSE) {
    DEVICE_RESPONSE_RESPCMD =           0x80, // 收包确认
    DEVICE_RESPONSE_HISTORY_COUNT =     0X81, // 历史数据个数
    DEVICE_RESPONSE_HISTORY_DATA =      0X82, // 历史数据上报
    DEVICE_RESPONSE_OXYGEN_HRV =        0x83, // 上报心率血氧
    DEVICE_RESPONSE_STEP_DATA =         0X84, //计步数据
    DEVICE_RESPONSE_TEMPRETRUE_DATA =   0X85, // 体表温度
    DEVICE_RESPONSE_BATTERY_LEVEL =     0X86, //电池电量和充电
    DEVICE_RESPONSE_DEVICEINFO_DATA =   0X87, //设备信息上报
    DEVICE_RESPONSE_SN_DATA =           0X88, //设备信息SN上报
    DEVICE_RESPONSE_SOS =               0X89, //sos触发上报
    DEVICE_RESPONSE_SOS_MEASUREDATA =   0X8A, //sos测量数据
    DEVICE_RESPONSE_MANUFAT_R1 =        0x8D, //上报厂家验证 R1
    DEVICE_RESPONSE_MANUFAT_RES =       0x8E, // 上报厂家验证结果
    DEVICE_RESPONSE_HR_MEASURE_DURATION =       0x8F, // 设备信息5 上报设备心率测量时长
    DEVICE_RESPONSE_HISTORY_DATA_2 =      0X91, // 历史数据上报, 包含运动模式
    DEVICE_RESPONSE_HISTORY_DATA_3 =      0X92, // 历史数据上报, 计步数据不用修正
    
    DEVICE_RESPONSE_ECG_ROW =               0X94, // ecg上报 原始数据
    DEVICE_RESPONSE_ECG_PPG_ROW =           0X95, // ecg+ppg上报 原始数据
    DEVICE_RESPONSE_ECG_PPG_FILTER =           0X96, //ecg 算法滤波后波形数据
    DEVICE_RESPONSE_ECG_RESULT = 0X97, //ecg算法计算结果
    DEVICE_RESPONSE_ECG_LEAD_ON = 0X98, // ecg手指接触状态
    /* CM */
    DEVICE_RESPONSE_SR28_MUTI_INFO = 0xC0, // 用户信息上报 / 测量时间+间隔 / 用哪种算法
     
    DEVICE_RESPONSE_HISTORY_DATA_SR28 = 0xC1, // 历史数据 设备使用CM算法
    DEVICE_RESPONSE_ACT_SWIM_SLEEP_HIS_CNT = 0xC2, // 上报活动检测/游泳/睡眠历史数据个数
    
    DEVICE_RESPONSE_AUTO_DETEC_ACTIVITY_DATA = 0xC3, // 活动自检数据
    DEVICE_RESPONSE_AUTO_DETEC_SWIM_DATA = 0xC4, // 游泳自检数据
    
    DEVICE_RESPONSE_ACTIVITY_DURIN_EXERCISE = 0xC5,// 锻炼期间的活动数据
    DEVICE_RESPONSE_SWIM_SUM = 0xC6, // 游泳锻炼总的游泳结果数据
    DEVICE_RESPONSE_SWIM_SINGLE_LAP = 0xC7, // 游泳锻炼单圈数据
    

    DEVICE_RESPONSE_LAST_ACTIVITY = 0xC8, // 最后的活动上报
    DEVICE_RESPONSE_CM_SLEEP =  0xC9, // 算法输出的睡眠数据
    DEVICE_RESPONSE_HIS_ACTIVITY = 0xCA, // 活动 在活动历史中上报 格式和 0xC8相同
    
    DEVICE_RESPONSE_CM_REALMEAS_DATA = 0xCB, // cm设备实时测量数据
    
    DEVICE_RESPONSE_EXER_VITAL_SIGNS = 0xCC, // 锻炼期间的生命体征数据
    
    DEVICE_RESPONSE_ACTIVITY_REALTIME_DUR_EXER = 0xCD,// 实时上报锻炼期间的活动数据
    DEVICE_RESPONSE_SKINTEMP_DUR_ASLEEP = 0xCE,  // 设备检测到睡眠期间的体温数据
    DEVICE_RESPONSE_LED_MEA_DATA = 0xD0,  // 单色 led测量上报
    DEVICE_RESPONSE_ACTIVITY_INTNSTY_HISDATA = 0xD1, // 步数/体温/活动强度历史数据 sr28c
    DEVICE_RESPONSE_PPG_STATA = 0xD2, // ppg测量中的状态上报
    
    DEVICE_RESPONSE_LAST_ACTIVITY_2 = 0xD3, // 最后的活动上报2
    DEVICE_RESPONSE_HIS_ACTIVITY_2 = 0xD4, // 活动 在活动历史中上报 格式和 0xD3相同
    DEVICE_RESPONSE_HIS_ERR_LOG = 0xD5, // 设备记录的错误log
}; // 设备上报协议


typedef NS_ENUM(uint8_t, SDKSportType) { // 下发运动类型
    SDK_SP_OTHER = 0,  // 除了跑步以外的
    SDK_SP_RUNNING = 1, // 跑步
};

@interface CAPacket : NSObject

@property(weak, nonatomic)id<StLeKeyFobProtocol> dataDelegate;

@property(assign, nonatomic)BOOL isTranslatingHistory; //标记是否传输历史数据

@property(strong, nonatomic)NSTimer *hisTimer;

@property(assign, nonatomic, readonly)uint8_t projectId;


-(void)resetChargeState;
-(void)resetTranslatingHistoryState;

/// 获取心率血氧数据
/// @param isOpen 开关
//+(NSData *)packGetOxygenHrv:(BOOL)isOpen;
+(NSData *)packGetOxygenHrvBoth:(BOOL)both IsOpen:(BOOL)isOpen;


-(void)receiveData:(NSData *)data;
/// 重启
+(NSData *)packReboot;
/// 关机
+(NSData *)packShutDown;
/// 获取电量和充电状态
+(NSData *)packBattery;

+(NSData *)packSN;
// 历史个数
+(NSData *)packHistoryCount;
/// 下发 led单色测量
/// - Parameters:
///   - on: 1开,0关
///   - ledColor: 0-绿灯,1-红灯,2-红外
///   - autoAdjBri: 是否自动调光
///   - current: 电流 0-255
///   - sampleRate: 0-25hz, 1-50hz, 2-100hz
+(NSData *)packLedMeasure:(BOOL)on LedColor:(uint8_t)ledColor AutoAdjBri:(BOOL)autoAdjBri Current:(uint8_t)current SampleRate:(uint8_t)sampleRate;

// 历史数据
+(NSData *)packHistoryData;
// 清空设备历史
+(NSData *)packClearHistory;

+(NSData *)packDevInfo;

+(NSData *)packTimeSync:(NSDate *)date; //时间同步
+(NSData *)packBindDevice:(BOOL)bind;

/// oem认证开关
/// @param on: YES = 开, NO = 关
+(NSData *)packOEMSwitch:(BOOL)on;

+(NSData *)packGetSteps;
+(NSData *)packGetTemperature;
+(NSData *)PackReset;
// 下发获取设备信息5
+(NSData *)PackGetDevInfoFive;

// 下发设置测量时长 单位 秒 8-180s
+(NSData *)PackSetHrMeasureDuration:(NSInteger)seconds;

// 血氧测量设置
+(NSData *)packSettingOxygenMeasure:(NSUInteger)startHor StartMin:(NSUInteger)startMin
                           StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor
                             EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec
                                Interval:(NSUInteger)interval IsOn:(BOOL)isOn;
// 心率 体温设置
+(NSData *)packSettingHeartRateMeasurInterval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval IsOn:(BOOL)isOn;

+(NSData *)packSportMode:(NSUInteger)startHor StartMin:(NSUInteger)startMin
                StartSec:(NSUInteger)startSecond Endhor:(NSUInteger)endHor
                  EndMin:(NSUInteger)endMin EndSec:(NSUInteger)endSec
              HrIntreval:(NSUInteger)hrInterval SkinTemperatureInterval:(NSUInteger)skinTemperatureInterval
                BloodOxygenInterval:(NSUInteger)oxygeninterval
                                IsOn:(BOOL)isOn;

+(NSData *)packSOSSettingDoubleTapTimes:(uint8_t)doubleTapTimes
                                       SingleTapInterval:(uint8_t)singleTapInterval TapDetectionThreshold:(uint8_t)tapDetectionThreshold
           DoubleTapEffectStartTime:(uint8_t)doubleTapEffectStartTime DoubleTapEffectEndTime:(uint8_t)doubleTapEffectEndTime Open:(BOOL)sosOpen;

+(uint8_t)crc8:(NSData *)packData;
+(BOOL)crc8Checker:(NSData *) packData;

// 运动模式
+(NSData *)packSoprtMode:(BOOL)start
           ValueINterval:(uint16_t)valInterval
                Duration:(uint16_t)durationMIn
               SportType:(SDKSportType)spType;

+(NSData *)PackEcgAndPpgMeasure:(BOOL)on;

+(NSData *)PackEcgMeasure:(BOOL)on SampleRate:(uint16_t)sampleRate InnerClockSource:(uint8_t)clockSource EcgPgaGain:(uint8_t)ecgPgaGain DispSrc:(BOOL)isFilterWave;

+(NSData *)PackSetStepCountingThreshold:(uint8_t)threshold;


/* AES  */

+(NSData *)packStartAes;
/// 下发解密原文
+(NSData *)packSendR2:(NSData *)r2;

/// 打包修改的蓝牙广播名
/// - Parameter name: 蓝牙广播名
+(NSData *)packModifyBRcastName:(NSString *)name HideMac:(BOOL)hideMac;


/* AES  */

/* CM */
// 设置用户信息
+(NSData *)packSetUsrInfoToDev:(NSUInteger)gender Age:(NSUInteger)ageYear Height:(NSUInteger)height Weight:(NSUInteger)weight IsGet:(BOOL)isGet;

+(NSData *)packGetLastActivity;

// 清楚活动历史数据
+(NSData *)packCleanActivityHis;

// 获取活动+游泳+睡眠 历史个数
+(NSData *)packGetHisCntOfActivAndSwimAndSleep;

// 获取活动+游泳+睡眠 历史数据
+(NSData *)packGetHisDataOfActivAndSwimAndSleep:(uint32_t)limitUUid;

+(NSData *)packCmDura:(BOOL)isSet Type:(uint32_t)type  DuraNoSlp:(uint32_t)meaduraNoSleep  IntervNoslp:(uint32_t)meaIntervNoSleep DuraInSlp:(uint32_t)meaDuraInSleep IntervInslp:(uint32_t)meaIntervInSlp;

+(NSData *)packCMexercise:(uint8_t)subCmd ExerciseType:(uint8_t)execType PoolSize:(uint8_t)sizeType HeartrateStorageInterval:(uint16_t)hrStoreInterv DurationMins:(uint16_t)duraMins;

+(NSData *)packexerciseRealReportSwitch:(BOOL)on;

+(NSData *)packDeviceAlgorithm:(BOOL)isSet Type:(uint8_t)algorithmType;

+(NSData *)packGetLastActivity_2;

/* CM */
@end

NS_ASSUME_NONNULL_END
